using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using HDL_ON.DAL.Server; using HDL_ON.Entity; using HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock; using Shared; using static HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock.CommonMethod; namespace HDL_ON.UI.UI2.FuntionControlView.HisenseTV { public class Send { private static Send sendMethod = null; /// /// 获取对象 /// public static Send Current { get { if (sendMethod == null) { sendMethod = new Send(); } return sendMethod; } } /// /// 打开海信电视 /// /// 电视mac public void Open(string deviceMac) { CommonMethod.Current.SunThread(() => { // 创建UdpClient实例 UdpClient udpClient = new UdpClient(); try { // 设置发送和接收数据的IP和端口 IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 9); // 发送数据 //byte[] data = System.Text.Encoding.ASCII.GetBytes("Hello, UDP Server!"); byte[] data = StrToToHexByte(deviceMac); for (int i = 0; i < 16; i++) { udpClient.Send(data, data.Length, iPEndPoint); //Console.WriteLine("数据已发送"); } //// 接收数据 //IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); //byte[] receivedData = udpClient.Receive(ref remoteEP); //string receivedMessage = System.Text.Encoding.ASCII.GetString(receivedData); //Console.WriteLine("接收到的数据:" + receivedMessage); } catch (Exception e) { Console.WriteLine("发生错误:" + e.Message); } finally { // 关闭UdpClient连接 udpClient.Close(); } }, TipType.none); } /// /// 刷新设备状态 /// /// 是否需要提示,默认提示 /// public bool RefreshDeviceStatus(List functionIds, TipType tipType = TipType.flicker) { try { Dictionary d = new Dictionary(); d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id); d.Add("deviceIds", functionIds); var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_RefreshDeviceStatus, "刷新设备状态"); if (!this.DataChecking(responsePackNew, tipType)) { return false; } return true; } catch { return false; } } /// /// 获取设备详情 /// /// 是否需要提示,默认提示 /// public Function GetDeviceInfo(string functionId, TipType tipType = TipType.flicker) { Dictionary d = new Dictionary(); d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id); d.Add("deviceIds", new List() { functionId }); var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_GetDevcieInfoList, "获取设备详情"); if (!this.DataChecking(responsePackNew, tipType)) { return null; } var functionList = Newtonsoft.Json.JsonConvert.DeserializeObject>(responsePackNew.Data.ToString()); if (functionList != null && functionList.Count > 0) { return functionList[0]; } return null; } /// ///删除设备 /// /// 设备id /// 成功返回true /// 是否需要提示,默认提示 public void DelDevice(string deviceId, Action action, TipType tipType = TipType.flicker) { Dictionary d = new Dictionary(); d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id); d.Add("deviceIds", new List { deviceId });//设备id var responsePackNew = UI.Music.SendMethod.Current.RequestServerhomeId(d, NewAPI.Api_Post_UnbindDeviceToHome, "删除海信电视"); if (!this.DataChecking(responsePackNew, tipType)) { //action?.Invoke(false); return; } action?.Invoke(true); } /// /// 发送控制命令 /// /// 当前设备 /// 发送控制数据 public void SendControlCommand(Function device, Dictionary dic,Action action) { new System.Threading.Thread(() => { var result = DriverLayer.Control.Ins.SendWriteCommand(device, dic, false, 0); action?.Invoke(result); }) { IsBackground = true }.Start(); } /// /// 将16进制的字符串转为byte[] /// /// /// private byte[] StrToToHexByte(string hexDeviceMacString) { if (string.IsNullOrEmpty(hexDeviceMacString)) { return new byte[] { }; } hexDeviceMacString = hexDeviceMacString.Replace(" ", "").Replace("_",""); if ((hexDeviceMacString.Length % 2) != 0) { return new byte[] { }; } byte[] returnBytes = new byte[hexDeviceMacString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexDeviceMacString.Substring(i * 2, 2), 16); return returnBytes; } /// /// 二进制数组转十六进制字符串 /// /// /// private string Byte2hex(byte[] btyes) { StringBuilder sb = new StringBuilder(btyes.Length * 2); for (int i = 0; i < btyes.Length; i++) { int hight = ((btyes[i] >> 4) & 0x0f); int low = btyes[i] & 0x0f; sb.Append(hight > 9 ? (char)((hight - 10) + 'a') : (char)(hight + '0')); sb.Append(low > 9 ? (char)((low - 10) + 'a') : (char)(low + '0')); } return sb.ToString(); } /// ///请求服务器(与住宅有关:例如;homeId) /// /// 发送数据 /// 请求地址(不是绝对地址) /// 标记->描述接口(自定义) /// public ResponsePackNew RequestServerhomeId(object o, string api_Url, string tag, int mTimeout = 3) { Log($"{DateTime.Now}->发送->{tag}", api_Url, o.ToString()); var requestJson = HttpUtil.GetSignRequestJson(o); var r = HttpUtil.RequestHttpsPostFroHome(api_Url, requestJson, mTimeout); Log($"{DateTime.Now}->回复->{tag}", "", Newtonsoft.Json.JsonConvert.SerializeObject(r)); return r; } /// /// /// 检验数据回复成功或者失败 /// /// 回复数据对象 /// 是否需要提示 /// private bool DataChecking(ResponsePackNew responsePackNew, TipType tipType) { if (responsePackNew.Data == null || responsePackNew.Code != "0" || responsePackNew.Data.ToString() == "") { if (TipType.flicker == tipType) { if (responsePackNew == null || responsePackNew.Code == "-1") { responsePackNew = new ResponsePackNew { message = "没回复,请确认网络是否正常.", Code = "-1", }; } Application.RunOnMainThread(() => { //new Tip() //{ // CloseTime = 1, // Text = responsePackNew.message + "(" + responsePackNew.Code + ")", // Direction = AMPopTipDirection.None, //}.Show(MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1)); CommonMethod.Current.ShowTip(responsePackNew.message + "(" + responsePackNew.Code + ")", 3); }); } return false; } return true; } /// /// 打印日志 /// /// 打印标记 /// 打印内容 public void Log(string tag, string url, string content) { #if DEBUG Console.WriteLine(tag + $"\r\n{url}\r\n{content}"); #endif } } }